
import 'package:dds/EventDispatcher.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

class MyBtn extends StatelessWidget{

  final String data;
  MyBtn(this.data, {super.key});
  late Function onPress;

  @override
  Widget build(BuildContext context) {
    return ElevatedButton(onPressed: (){
      onPress(data);
    }, child: Text(data));
  }
}

class BodyWidget{

  TextEditingController textController=TextEditingController();
  TextEditingController textController2=TextEditingController();
  List<String> chars=["0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f"];
  RxString stateInfo="connect".obs;
  String eventName="";

  List<Widget> getColumns(){
    List<Widget> rows=[];
    int j=0;
    int n=0;
    for(int i=0; i<4; i++){
      List<MyBtn> btns=[];
      for(j=0; j<4; j++){
        MyBtn btn=MyBtn(chars[n]);
        btn.onPress=(data){
          textController.text=data;
          sendDataEvent(data);
        };
        btns.add(btn);
        n++;
      }
      rows.add(Row(children: btns,));
    }
    return rows;
  }

  void showConnectState(String info, int flag){
    if(flag==1){
      stateInfo.value="disconnect";
    }else{
      stateInfo.value="connect";
    }
    showCallbackInfo(info);
  }

  void showCallbackInfo(String info){
    textController2.text="$info\n${textController2.text}";
  }

  void sendDataEvent(String msg){
    if(msg.trim().isNotEmpty){
      EventDispatcher.dispatchEvent("sendData_$eventName",msg);
    }
  }

  Widget getWidget(RxString theStr,String evtName){
    eventName=evtName;
    return Padding(padding: const EdgeInsets.all(10),child: SingleChildScrollView(
      child:Column(children: [
        Row(mainAxisAlignment: MainAxisAlignment.spaceBetween,children: [
          Align(
            alignment: Alignment.centerLeft,
            child:ElevatedButton(onPressed: (){
              textController2.text="";
            }, child:const Text("clear")),
          ),
          const Flexible(fit: FlexFit.tight,child: SizedBox(),),
          Align(
            alignment: Alignment.centerRight,
            child:ElevatedButton(onPressed: (){
              if(stateInfo.value=="connect"){
                EventDispatcher.dispatchEvent("showDialog_$eventName","");
              }else{
                EventDispatcher.dispatchEvent("disconnect_$eventName","");
                stateInfo.value="connect";
                showCallbackInfo("disconnect");
              }
            }, child:Obx(()=>Text(stateInfo.value))),
          ),
        ],
        ),
        Row(children: [
          Expanded(
              child: TextField(controller: textController2,
                keyboardType: TextInputType.multiline,
                maxLines: 5,
                decoration: const InputDecoration(
                  border: OutlineInputBorder(),
                ),
                readOnly: true,
              )),
        ],),
        const Row(children: [SizedBox(height: 30,)],),
        Row(children: [
          Expanded(
            child: TextField(
                controller: textController,
                decoration: const InputDecoration(
                  fillColor: Color(0x33cccccc),
                  filled: true,
                  border: OutlineInputBorder(),
                  contentPadding: EdgeInsets.symmetric(vertical: 5, horizontal: 10),
                  isCollapsed: true,
                )
            ),
          ),
          ElevatedButton(onPressed: (){
            print("发送");
            sendDataEvent(textController.text);
          }, child:const Text("send"))
        ],),
        const Row(children: [SizedBox(height: 30,)],),

        Row(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Center(child: Obx((){
              if(theStr.value=="pan"){
                return Column(children: [
                  Row(children: [ElevatedButton(onPressed: (){
                    textController.text="F";
                    sendDataEvent("F");
                    }, child: const Text("F"))],),
                  Row(children: [
                    ElevatedButton(onPressed: (){
                      textController.text="L";
                      sendDataEvent("L");
                      }, child: const Text("L")),
                    const SizedBox(width: 100,height: 50,),
                    ElevatedButton(onPressed: (){
                      textController.text="R";
                      sendDataEvent("R");
                      }, child: const Text("R")),
                  ]),
                  Row(children: [ElevatedButton(onPressed: (){
                    textController.text="B";
                    sendDataEvent("B");
                    }, child: const Text("B"))],),
                ]);
              }else{
                return Column(children: getColumns());
              }
            }),),
          ],
        ),
      ],),
    )
    );
  }
}